package com.cardbaobao.cardbabyclient.ui.adapter.base;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * ViewPager通用
 * <p>
 * Author: FHW
 * Date: 2017-06-12
 */

public class CommonViewPagerAdapter extends FragmentStatePagerAdapter {
    private String[] title;
    private List<Fragment> mFragments = new ArrayList<>();
    private List<Fragment> fragmentList;

    public CommonViewPagerAdapter(FragmentManager fm, String[] titles) {
        super(fm);
        title = titles;
    }
    public CommonViewPagerAdapter(FragmentManager fm, String[] titles,List<Fragment> fragments) {
        super(fm);
        title = titles;
        this.mFragments = fragments;
        notifyDataSetChanged();
    }

    public void addFragment(Fragment fragment) {
        mFragments.add(fragment);
    }

    public void cleanFragment() {
        if (mFragments != null) {
            mFragments.clear();
        }
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }
    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }
}
